package cn.ffcs.uec.staff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.organization.constant.UecOrganizationConstant;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uec.staff.action.bean.UecStaffSalaryChangesMainBean;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.vo.UecStaffInfoVo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecStaffSalaryChangesMainComposer extends BasePortletComposer implements IPortletInfoProvider {

	private static final long serialVersionUID = 3918743789180685542L;

	@Getter
	@Setter
	private UecStaffSalaryChangesMainBean bean = new UecStaffSalaryChangesMainBean();

	/**
	 * 组织树：组织+部门
	 */
	private OrganizationRelationVo organizationRelationVo;

	/**
	 * 选中的组织/部门 组织与部门分别在两张表中
	 */
	private UecOrgOrgs uecOrgOrgs;
	private UecOrgDept uecOrgDept;
	
	private UecStaffInfo uecStaffInfo;

	/**
	 * 
	 */
	private boolean salaryChangesFlag;
	/**
	 * 
	 */
	private boolean salaryChangesCleanFlag = false;

	/**
	 * 外包人员虚拟对象
	 */
	private UecStaffInfoVo uecStaffInfoVo = new UecStaffInfoVo();

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");
	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");

	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		this.bean.getUecStaffInfoListboxComp().setPortletInfoProvider(this);
		this.bean.getUecPsndocWadocListboxExt().setPortletInfoProvider(this);
		/**
		 * 选中内部树上的（组织关系）
		 */
		this.bean.getUecOrganizationTreeboxExt().addForward(UecOrganizationConstant.ON_SELECT_ORGREATION_TREE_REQUEST,
				this.self, "onSelectOrgOrDeptTreeResponse");

		this.bean.getUecStaffInfoListboxComp().addForward("onSalaryChangeUecStaffInfoQuery", this.self,
				"onSalaryChangeUecStaffInfoQueryResponse");
		this.bean.getUecStaffInfoListboxComp().addForward("onCleanSalaryChangeUecStaffInfoQuery", this.self,
				"onCleanSalaryChangeUecStaffInfoQueryResponse");
	}

	/**
	 * 界面初始化
	 */
	public void onCreate$uecStaffSalaryChangesMainWindow() {
		salaryChangesFlag = true;
		Events.postEvent("onSalaryChangesFlag", this.bean.getUecStaffInfoListboxComp(), salaryChangesFlag);
	}

	/**
	 * 选中的内部树组织关系
	 * 
	 * @param event
	 */
	public void onSelectOrgOrDeptTreeResponse(ForwardEvent event) {
		organizationRelationVo = (OrganizationRelationVo) event.getOrigin().getData();
		if (null != organizationRelationVo) {
			this.organizationVoTransfOrgOrDept(organizationRelationVo);
			organizationRelationVo = null;
		}
	}

	/**
	 * 根据组织关系获取各对象（组织还是部门）
	 */
	public void organizationVoTransfOrgOrDept(OrganizationRelationVo organizationRelationVo) {
		if ("ORG".equals(organizationRelationVo.getOrgType())) {
			uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByOrganizationRelationVo(organizationRelationVo);
			// 公司
			//uecStaffInfoVo.setPkOrg(uecOrgOrgs.getPkOrg());
			String orgName=uecOrgOrgs.getName();
       	 if("中国电信股份有限公司内蒙古分公司".equals(orgName)) {
       		 uecStaffInfoVo
       		.setOrgName(null);
       	}else {
       		uecStaffInfoVo
       		.setOrgName(orgName);
       	}
			
		} else {
			uecStaffInfoVo.setPkOrg(null);
		}
		if ("DEPT".equals(organizationRelationVo.getOrgType())) {
			uecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrganizationRelationVo(organizationRelationVo);
			// 部门
			uecStaffInfoVo.setPkDept(uecOrgDept.getPkDept());
		} else {
			uecStaffInfoVo.setPkDept(null);
		}
		Events.postEvent("onOrgTreeStaffQuery", this.bean.getUecStaffInfoListboxComp(), uecStaffInfoVo);
	}
	
	/**
	 * 将数据抛给合同制薪资变动ext
	 * @param event
	 */
	public void onSalaryChangeUecStaffInfoQueryResponse(ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		if (null != uecStaffInfo) {
			Events.postEvent("onSalaryChangeQuery", this.bean.getUecPsndocWadocListboxExt(), uecStaffInfo);
		}
	}
	
	public void onCleanSalaryChangeUecStaffInfoQueryResponse(ForwardEvent event) {
		salaryChangesCleanFlag = (Boolean) event.getOrigin().getData();
		if(salaryChangesCleanFlag) {
			Events.postEvent("onCleanSalaryChangeQuery", this.bean.getUecPsndocWadocListboxExt(), salaryChangesCleanFlag);
		}
	}

}
